import java.awt.*;
import java.io.Serializable;
import java.util.ArrayList;

import javax.swing.JPanel;

/**
 * represents a Player for a M.U.L.E. game
 * 
 * @author Sherman Mathews
 */
public class Player implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;		//used for saving
	private int[] playerResources = new int[6];				//an array of player resources
	private int money;										//how much money the player has
	private Property[] playerProperties = new Property[44];	//an array of player properties
	private String name;									//the player's name												
	private Color color;									//the player's color
	private String race;  									//the player's race
	private boolean partialShortage;						//if the player has a partial shortage
	private int tableLim;									//determines limit for shortage
	private boolean totalShortage;							//if the player has a total shortage
	private int turnTime;									//how long the player's turn is
	private ArrayList<Mule> mules;							//the player's mules

	/*for resources array -- 0: food, 1: energy, 2: smith ore, 3: MULE*/

	/**
	 * creates an instance of a player in a M.U.L.E. game
	 * @param color the player's color
	 * @param race the player's race
	 */
	public Player(Color color, String race, String name){
		this.color = color;
		this.race = race;
		this.name = name;
		mules = new ArrayList<Mule>();

		playerResources[0] = 4;		//food
		playerResources[1] = 2;		//energy
		playerResources[2] = 0;		//ore
		playerResources[3] = 0;		//MULE w/ food
		playerResources[4] = 0;		//MULE w/ energy
		playerResources[5] = 0;		//MULE w/ smithore

		//money changes based on race
		if (GameModel.getInstance().getDifficulty().equalsIgnoreCase("Easy")) {
			if(race.equals("Human")){
				setMoney(600);
			}
	
			else if(race.equals("Flapper")){
				setMoney(1600);
			}
	
			else if(race.equals("Alien"))
			{
				setMoney(800);
			}
	
			else if(race.equals("Batman"))
			{
				setMoney(1200);
			}
	
			else{
				setMoney(1000);
			}
		}
		else {
			if (race.equalsIgnoreCase("Human"))
				setMoney(200);
			else if(race.equals("Flapper"))
				setMoney(1200);
			else if(race.equals("Alien"))
				setMoney(400);
			else if(race.equals("Batman"))
				setMoney(800);
			else
				setMoney(600);
		}
	}
	/**
	 * determines if player has a mule
	 * @return true if player has a mule, false otherwise
	 */
	public boolean hasMule(){
		return(playerResources[3] > 0);
	}

	/**
	 * Returns the name of the Player
	 * @return String name of the player
	 */
	public String getName() {
		return name;
	}

	/**
	 * moves the player
	 * @param e a key press
	 */
	public void move(Event e){
		//code
	}

	/**
	 * gets the player's money
	 * @return player's money
	 */
	public int getMoney() {
		return money;
	}

	/**
	 * sets the player's money
	 * @param money amount to be set
	 */
	public void setMoney(int money) {
		this.money = money;
	}

	/**
	 * gets the player's properties
	 * @return an array of the player's properties
	 */
	public Property[] getPlayerProperties() {
		return playerProperties;
	}

	/**
	 * sets the player's properties
	 * @param playerProperties the player's current properties
	 */
	public void setPlayerProperties(Property[] playerProperties) {
		this.playerProperties = playerProperties;
	}

	/**
	 * gets the player's color
	 * @return player's color
	 */
	public Color getColor() {
		return color;
	}

	/**
	 * gets the player's race
	 * @return player's race
	 */
	public String getRace() {
		return race;
	}

	/**
	 * Adds a property to the Player's property array
	 * @param property the property to be added
	 */
	public void addProperty(Property property) {
		if(property != null)
		{
			int i = 0;
			while( playerProperties[i] != null){
				i++;
			}
			playerProperties[i] = property;
		}
		else
			return;
	}

	/**
	 * returns if a player has a partial shortage
	 * @return boolean if player has a partial shortage
	 */
	public boolean isPartialShortage() {
		if(playerResources[0] > 0 && playerResources[0] < tableLim){
			return true;
		}
		return false;
	}

	/**
	 * returns if a player has a total shortage
	 * @return boolean if player has a total shortage
	 */
	public boolean isTotalShortage() {
		if(playerResources[0] == 0){
			return true;
		}
		return false;
	}

	/**
	 * sets the tableLimit that determines partial/total shortage
	 * @param num the tableLimit to be set to
	 */
	public void setTableLim(int num){
		tableLim = num;
	}

	/**
	 * sets the amount of time for a player's turn
	 * @param time the amount of time to set it to
	 */
	public void setTurnTime(int time){
		this.turnTime = time;
	}

	/**
	 * returns the amount of time for a player's turn
	 * @return int the amount of time for a player's turn
	 */
	public int getTurnTime() {
		return turnTime;
	}

	/**
	 * returns the amount of a certain resource a player has
	 * @param i the resource number
	 * @return the amount of the resource
	 */
	public int getPlayerResource(int i){
		return playerResources[i];
	}

	/**
	 * Sets the  player's resource count to a certain number 
	 * @param itemNumber the item to update
	 * @param amount the amount to update it to
	 */
	public void setPlayerResource(int itemNumber, int amount){
		playerResources[itemNumber] = amount;
	}
	/**
	 * A to string for the colors of the players
	 * @return String the color of the player
	 */
	public String colorToString() {
		String string = "";
		if (getColor() == GameModel.ourBlue)
			string = "Blue";
		else if (getColor() == GameModel.ourRed)
			string = "Red";
		else if (getColor() == GameModel.ourYellow)
			string = "Yellow";
		else if (getColor() == GameModel.ourPurple)
			string = "Purple";
		else if (getColor() == GameModel.mountainBrown)
			string = "Brown";
		else if (getColor() == GameModel.plainGreen)
			string = "Green";

		return string;
	}

	/**
	 * A list of the mules the player owns
	 * @return the player's mules
	 */
	public ArrayList<Mule> getMules(){
		return mules;
	}

	/**
	 * adds the mule to the player's mule list
	 * @param mule the mule to be added
	 */
	public void addMule(Mule mule){
		mules.add(mule);
	}

	/**
	 * removes the mule from the player's mule list
	 * @param mule the mule to be removed
	 */
	public void removeMule(Mule mule){
		mules.remove(mule);
	}

	public void clearMules(){
		mules = new ArrayList<Mule>();
	}
}
